<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Tech extends MY_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct() {
        parent::__construct();
        $this->load->model('general_model');
        $this->per_page = 1;
        $data['tech'] = $this->general_model->get_row('techs', "user_id = {$this->user_id}");
    }

    public function index() {

        parent::core_tech_view('tech/body_view');
    }

    public function check_tool($check_id) {##กรอกแบบฟอร์มทำเบ้าขาเทียม
        if ($check_id == false) {
            redirect(base_url() . 'patient/list_teach', 'location');
            exit();
        }
        $data['check'][] = $this->general_model->get_row('check_pat', "check_id = {$check_id} "); ##
        $data['pat'] = $this->general_model->get_row('patients', "pat_id = {$data['check'][0]['pat_id']}");
        $data['evol'][] = $this->general_model->get_row('evolution', "pat_id = {$data['check'][0]['pat_id']} and check_id = {$data['check'][0]['check_id']}");
        $data['tool'] = $this->general_model->get_all('order_tools', "check_id = {$check_id}");
        foreach ($data['tool'] as $key => $value) {
            $data['tool_type'][] = $this->general_model->get_all('tools', "tool_id = '{$value['tool_id']}'");
        }
        parent::core_tech_view('tech/check_tool', $data);
    }

    public function query_check_tool($check_id, $order_id, $tool_type, $amp_id = false) {
        $data['check'][] = $this->general_model->get_row('check_pat', "check_id = {$check_id} "); ##
        $data['pat'] = $this->general_model->get_row('patients', "pat_id = {$data['check'][0]['pat_id']}");
        $data['tool'] = $this->general_model->get_all('order_tools', "check_id = {$check_id}");
        $data['tool_type'] = $this->general_model->get_row('tools', "tool_type = '{$tool_type}'");
        if ($amp_id == false) {
            $data['tool_detail'][] = $this->input->post();
        } else {
            $data['tool_detail'][] = $this->general_model->get_row($tool_type, "amp_id = '{$amp_id}'");
        }

        if ($this->input->post()) {
            $post = $this->input->post();
            array_pop($post);
            $post['tech_id'] = $this->user_id;
            $post['date'] = date('Y-m-d');
            if ($amp_id == false) {
                $this->general_model->insert($tool_type, $post);
                $amp_id = $this->db->insert_id();
                $data_order = array(
                    'amp_id' => $amp_id,
                    'tool_type' => $tool_type,
                    'order_status' => "กรอกแล้ว",
                    'tech_id' => $this->user_id,
                );
                $this->general_model->update('order_tools', $data_order, "order_id = {$order_id}");
                $data_check = array(
                    'check_status' => "กำลังจัดทำอุปกรณ์",
                );
                $this->general_model->update('check_pat', $data_check, "check_id = {$check_id}");
            } else {
                unset($post['date']);
                $data_order = array(
                    'order_status' => "กรอกแล้ว",
                    'tech_id' => $this->user_id,
                );
                $data_check = array(
                    'check_status' => "กำลังจัดทำอุปกรณ์",
                );
                $this->general_model->update('check_pat', $data_check, "check_id = {$check_id}");
                $this->general_model->update('order_tools', $data_order, "order_id = {$order_id}");
                $this->general_model->update($tool_type, $post, "amp_id = {$amp_id}");
            }
            redirect(base_url() . 'tech/check_tool_view/' . $check_id . '/' . $order_id . '/' . $tool_type . '/' . $amp_id);
        }
        parent::core_tech_view('tech/check_tool_add_edit', $data);
    }

    public function check_tool_view($check_id, $order_id, $tool_type, $amp_id = false) {##กรอกแบบฟอร์มทำเบ้าขาเทียม
        if ($amp_id == false) {
            $ref = $this->input->server('HTTP_REFERER', TRUE);
            ?>
            <meta charset="utf-8">
            <script type="text/javascript">
                var url = '<?php echo $ref; ?>';
                if (confirm("ยังไม่ได้กรอกแบบฟอร์ม ในอุปกรณืนี้ !") == true) {
                    window.location.href = url;
                } else {
                    window.location.href = url;
                }

            </script>

            <?php
        }
        $data['check'][] = $this->general_model->get_row('check_pat', "check_id = {$check_id} "); ##
        $data['pat'] = $this->general_model->get_row('patients', "pat_id = {$data['check'][0]['pat_id']}");
        $data['tool'] = $this->general_model->get_all('order_tools', "check_id = {$check_id}");
        $data['tool_type'] = $this->general_model->get_row('tools', "tool_type = '{$tool_type}'");
        $data['tool_detail'][] = $this->general_model->get_row($tool_type, "amp_id = '{$amp_id}'");
        foreach ($data['tool'] as $key => $value) {
            $data['tool_type'][] = $this->general_model->get_all('tools', "tool_id = '{$value['tool_id']}'");
        }
        parent::core_tech_view('tech/check_tool_view', $data);
    }

    public function owner_tools($page = false) {
        /*         * pagination */
        $this->per_page = 20;
        $row = $this->general_model->QueryAll("
                                                            select * from order_tools t1 
                                                                            inner join check_pat t2 on t1.check_id = t2.check_id
                                                                            inner join patients t3 on t3.pat_id = t2.pat_id 
                                                                            where 
                                                                                t1.tech_id={$this->user_id} 
                                                                                and t2.check_status != 'ประเมิณเรียบร้อยแล้ว'
                                                                            order by t2.date
                                                          ", 'count');
        $url = base_url() . 'patient/index/';
        parent::pagination($page, $url, $row, $this->per_page, 3);
        /*         * pagination */
        $data['patient'] = $this->general_model->QueryAll("
                                                            select * from order_tools t1 
                                                                            inner join check_pat t2 on t1.check_id = t2.check_id
                                                                            inner join patients t3 on t3.pat_id = t2.pat_id 
                                                                            where 
                                                                                t1.tech_id={$this->user_id} 
                                                                                and t2.check_status != 'ประเมิณเรียบร้อยแล้ว'
                                                                            order by t2.date
                                                                            limit {$this->start_no}, {$this->per_page} 
                                                          ");

        // var_dump($row);die();
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('pat/list_pat', $data);
    }

    public function owner_tools_success($page = false) {
        /*         * pagination */
        $this->per_page = 20;
        $row = $this->general_model->QueryAll("
                                                            select * from order_tools t1 
                                                                            inner join check_pat t2 on t1.check_id = t2.check_id
                                                                            inner join patients t3 on t3.pat_id = t2.pat_id 
                                                                            where 
                                                                                t1.tech_id={$this->user_id} 
                                                                                and t2.check_status = 'ประเมิณเรียบร้อยแล้ว'
                                                                            order by t2.date
                                                          ", 'count');
        $url = base_url() . 'patient/index/';
        parent::pagination($page, $url, $row, $this->per_page, 3);
        /*         * pagination */
        $data['patient'] = $this->general_model->QueryAll("
                                                            select * from order_tools t1 
                                                                            inner join check_pat t2 on t1.check_id = t2.check_id
                                                                            inner join patients t3 on t3.pat_id = t2.pat_id 
                                                                            where 
                                                                                t1.tech_id={$this->user_id} 
                                                                                and t2.check_status = 'ประเมิณเรียบร้อยแล้ว'
                                                                            order by t2.date
                                                                            limit {$this->start_no}, {$this->per_page} 
                                                          ");

        // var_dump($row);die();
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('pat/list_pat', $data);
    }

    public function tool_approv($check_id, $type) {

        $data_order = array(
            'order_status' => "ยืนยันแล้ว",
        );
        $this->general_model->update('order_tools', $data_order, "check_id = {$check_id}");

        if ($type == 'before') {
            $status = 'รอตรวจก่อนทำสวยงาม';
        } elseif ($type == 'after') {
            $status = 'รอตรวจหลังทำสวยงาม';
        } elseif ($type == 'evol') {
            $status = 'รอการประเมิณ';
        }

        $data_check = array(
            'check_status' => $status,
        );
        $this->general_model->update('check_pat', $data_check, "check_id = {$check_id}");


        redirect($this->input->server('HTTP_REFERER', TRUE) . '?alert=ยืนยันแล้ว', 'LOCATION');
    }

    ####
}
